package com.yycx.module.bus.provider.handler.haiying.order.invoice;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 待开票订单列表
 *
 * @author zyf
 */
@Component
public class HyInvoiceOrderTab1ListHandler implements PageInterceptor<HyOrder> {

    @Autowired
    private SysCompanyService companyService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery<HyOrder> cq, PageParams pageParams, EntityMap params) {
        cq.eq(HyOrder.class, "companyId");
        String orderNo=cq.getParams("orderNo");
        if (FlymeUtils.isNotEmpty(orderNo)) {
            cq.and(e -> e.likeLeft("ho.orderNo", orderNo).or().likeLeft("ho.outOrderNo", orderNo));
        }
        cq.eq("orderState", 2);
        cq.eq("invoiceState", 1);
        cq.eq("sendState", 1);
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrder> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            Long companyId=entityMap.getLong("companyId");
            SysCompany company=companyService.getById(companyId);
            entityMap.put("companyName", company.getCompanyName());
        }
        return result;
    }

}
